import 'package:aweb5/generated/json/somebody_official_plan_entity_helper.dart';
import 'package:aweb5/style.dart';
import 'package:aweb5/util/NaviUtil.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:aweb5/custom/dashed_rect.dart';
//import 'dash_list.dart';
import 'unedittable_dash_list.dart';
import 'package:aweb5/util/DialogUtil.dart';
import 'package:aweb5/style.dart';
import 'package:aweb5/bean/aplus/group_plan_list_entity.dart';
import 'package:load/load.dart';
import 'package:aweb5/Constants.dart';
import 'package:aweb5/util/toast_util.dart';
import 'package:aweb5/config/Config.dart';
import 'package:aweb5/util/HttpClient.dart';
import 'package:aweb5/bean/aplus/somebody_official_plan_entity.dart';
import 'package:aweb5/generated/json/common_result_entity_helper.dart';
import 'package:aweb5/bean/aplus/common_result_entity.dart';
import 'package:aweb5/widgetlib/menu_widget.dart';

class ViewSomebodyAndOfficialAssistPlan extends StatefulWidget {
  String id;
  String url;
  String name;
  String desc;
  String hasfollowName;
  String hasfollowUrl;
  List<DropdownMenuItem<String>> sortItems = [];
  String selectedSort = "";

  bool official;
  ViewSomebodyAndOfficialAssistPlan(
      {this.id,
      this.url,
      this.name,
      this.desc,
      this.hasfollowName,
      this.hasfollowUrl,
      this.sortItems,
      this.selectedSort,

        this.official
      });
  @override
  _ViewSomebodyAndOfficialAssistPlanState createState() =>
      _ViewSomebodyAndOfficialAssistPlanState();
}

class _ViewSomebodyAndOfficialAssistPlanState
    extends State<ViewSomebodyAndOfficialAssistPlan> {
  BuildContext context;
  List<SomebodyOfficialPlanDataSendTask> taskSend = null;
//  GroupPlanListDataMember groupPlanListDataMember = null;
  GroupPlanListDataFollow followdata = null;

  bool followed = false;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    WidgetsBinding.instance.addPostFrameCallback((timeStamp) {requetPlan(); });

  }

  @override
  Widget build(BuildContext context) {
    this.context = context;
    return Scaffold(
      backgroundColor: Colors.white,
      body: SafeArea(
          child: Stack(
        children: <Widget>[
          Column(
            children: <Widget>[todayplan(), Expanded(child: buildBody())],
          ),
          Positioned(
            bottom: ScreenUtil().setHeight(20),
            child: Container(
              height: ScreenUtil().setHeight(40),
              width: ScreenUtil().setWidth(414),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: <Widget>[
                   

                  Container(
                    width: ScreenUtil().setWidth(119),
                    height: ScreenUtil().setHeight(40),
                    alignment: Alignment.center,
                    decoration: BoxDecoration(
                        color: Color(0xffFFE9E8),
                        borderRadius:
                            BorderRadius.circular(ScreenUtil().setHeight(4)),
                        border: Border.all(color: Color(0xffF94F68))),
                    child: Text(
                      "取消",
                      style: TextStyle(
                          color: Color(0xffF94F68),
                          fontSize: ScreenUtil().setSp(16)),
                    ),
                  ),
                  SizedBox(
                    width: ScreenUtil().setWidth(20),
                  ),
//                  GestureDetector(
//                    child: Container(
//                      width: ScreenUtil().setWidth(124),
//                      height: ScreenUtil().setHeight(40),
//                      alignment: Alignment.center,
//                      decoration: redButtonStyle,
//                      child: Text(
//                        "跟随一次",
//                        style: TextStyle(
//                            color: Colors.white,
//                            fontSize: ScreenUtil().setSp(16)),
//                      ),
//                    ),
//                    onTap: () {
////                      _showFollowDialog();
//                      _showChangeFollowDialog();
//                    },
//                  ),

                  GestureDetector(

                    child: Container(
                      width: ScreenUtil().setWidth(119),
                      height: ScreenUtil().setHeight(40),
                      alignment: Alignment.center,
                      decoration: redButtonStyle,
                      child: Text(
                        "跟随",
                        style: TextStyle(
                            color: Colors.white,
                            fontSize: ScreenUtil().setSp(16)),
                      ),
                    ),
                    onTap: (){

                      if(widget.hasfollowName.isEmpty){

                        _showFollowDialog();

                      }else{

                        _showChangeFollowDialog();
                      }

                    },

                  )
                  ,
                  
                ],
              ),
            ),
          )
        ],
      )),
    );
  }

  Widget todayplan() {
    return Container(
        width: ScreenUtil().setWidth(414),
        color: Colors.white,
        padding: EdgeInsets.symmetric(
            horizontal: ScreenUtil().setWidth(16),
            vertical: ScreenUtil().setHeight(11)),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: <Widget>[
            Column(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              crossAxisAlignment: CrossAxisAlignment.start,
              children: <Widget>[
                Text(
                  "今日计划",
                  style: TextStyle(
                      color: Color(0xff101010),
                      fontWeight: FontWeight.bold,
                      fontSize: ScreenUtil().setSp(20)),
                ),
                Text(
                  "最多显示7天内的计划  ${Constants.weekday}",
                  style: TextStyle(
                    color: Color(0xff101010),
                    fontSize: ScreenUtil().setSp(14),
                  ),
                ),
              ],
            ),
          ],
        ));
  }

  Widget buildBody() {
    return Stack(
      children: <Widget>[
        Container(
//          color: Colors.grey,
          width: ScreenUtil().setWidth(414),
          padding: EdgeInsets.only(),
          child: Column(
            children: <Widget>[
              Container(
                height: ScreenUtil().setHeight(5),
                color: Color(0xfff0f0f0),
//                color: Colors.blue,
              ),
              SizedBox(
                height: ScreenUtil().setHeight(19),
              ),
              Row(
                children: <Widget>[
                  SizedBox(
                    width: ScreenUtil().setWidth(20),
                  ),
                  Text(
                    "已为您推荐${widget.name}的今日计划",
                    style: TextStyle(
                        color: Color(0xff101010),
                        fontSize: ScreenUtil().setSp(20),
                        fontWeight: FontWeight.bold),
                  ),
                ],
              ),

              SizedBox(
                height: ScreenUtil().setHeight(19),
              ),
              Container(
//                color: Color(0xffffFFA07A),
                width: ScreenUtil().setWidth(371),
                child: DashedRect(
                  color: Color(0xff979797),
                  strokeWidth: ScreenUtil().setHeight(1),
                  gap: ScreenUtil().setWidth(3),
                ),
              ),
              SizedBox(
                height: ScreenUtil().setHeight(19),
              ),

//              DashList()
              Expanded(
                  child: Container(
                color: Color(0xfffcfcfc),
                width: ScreenUtil().setWidth(414),
                child: Row(
                  children: <Widget>[
                    Expanded(
                        child: taskSend == null
                            ? Text("")
                            : UnedittableDashList(
                                taskSend,
                                url: widget.url,
                              )),
//                SizedBox(width: 5,)
                  ],
                ),
              ))
            ],
          ),
        ),
        Positioned(
          child: Image.asset(
            "assets/assist/biaoqian.png",
            width: ScreenUtil().setWidth(19),
            height: ScreenUtil().setHeight(19),
          ),
          right: ScreenUtil().setWidth(30),
        )
      ],
    );
  }

  _showFollowDialog() {
    DialogUtil.showdialog1(
        this.context,
        Container(
          width: ScreenUtil().setWidth(323),
          height: ScreenUtil().setHeight(233),
          decoration: BoxDecoration(
              color: Colors.white, borderRadius: BorderRadius.circular(4)),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.center,
            children: <Widget>[
              SizedBox(
                height: ScreenUtil().setHeight(24),
              ),
              Text(
                "您将永久跟随TA的每日运营计划",
                style: TextStyle(
                    color: Color(0xff000000),
                    fontSize: ScreenUtil().setSp(16),
                    fontWeight: FontWeight.bold),
              ),
              SizedBox(
                height: ScreenUtil().setHeight(24),
              ),
              Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: <Widget>[
                  CircleAvatar(
                    radius: ScreenUtil().setWidth(21.5),
                    backgroundImage: widget.official==1?AssetImage("assets/assist/aplus.png"):NetworkImage("${widget.url}"),
                  ),
                  SizedBox(
                    width: ScreenUtil().setWidth(12),
                  ),
                  Column(
                    children: <Widget>[
                      Text(
                        "${widget.name}",
                        style: TextStyle(
                            color: Color(0xff000000),
                            fontSize: ScreenUtil().setSp(18),
                            fontWeight: FontWeight.bold),
                      ),
//                      Container(
//                        alignment: Alignment.center,
//                        width: ScreenUtil().setWidth(56),
//                        height: ScreenUtil().setHeight(14),
//                        decoration: redButtonStyle,
//                        child: Text(
//                          "${widget.desc}",
//                          style: TextStyle(
//                              color: Colors.white,
//                              fontSize: ScreenUtil().setSp(10)),
//                        ),
//                      )
                    ],
                  )
                ],
              ),
              SizedBox(
                height: ScreenUtil().setHeight(33),
              ),
              Row(
                children: <Widget>[
                  SizedBox(
                    width: ScreenUtil().setWidth(33),
                  ),
                  GestureDetector(
                    child: Container(
                      width: ScreenUtil().setWidth(119),
                      height: ScreenUtil().setHeight(40),
                      decoration: redButtonStyleWithBorder,
                      alignment: Alignment.center,
                      child: Text(
                        "取消",
                        style: TextStyle(
                            color: Color(0xffF94F68),
                            fontSize: ScreenUtil().setSp(16)),
                      ),
                    ),
                    onTap: () {
                      NaviUtil.pop(context);
                    },
                  ),
                  SizedBox(
                    width: ScreenUtil().setWidth(15),
                  ),
                  GestureDetector(
                    child: Container(
                      width: ScreenUtil().setWidth(119),
                      height: ScreenUtil().setHeight(40),
                      decoration: redButtonStyle,
                      alignment: Alignment.center,
                      child: Text(
                        "确定",
                        style: TextStyle(
                            color: Colors.white,
                            fontSize: ScreenUtil().setSp(16)),
                      ),
                    ),
                    onTap: (){

                      NaviUtil.pop(context);
                      requetFollow();

                    },
                  ),
                ],
              )
            ],
          ),
        ),
        ScreenUtil().setHeight(233));
  }

  void _showChooseGroupDialog() {
    DialogUtil.showdialog1(
        context,
        Container(
          padding: EdgeInsets.symmetric(
              horizontal: ScreenUtil().setWidth(30),
              vertical: ScreenUtil().setHeight(30)),
          width: ScreenUtil().setWidth(329),
          height: ScreenUtil().setHeight(215),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: <Widget>[
              Text(
                "选择群",
                style: TextStyle(
                    color: Color(0xff101010),
                    fontSize: ScreenUtil().setSp(16),
                    fontWeight: FontWeight.bold),
              ),
              Expanded(child: Text("")),
              Container(
                padding: EdgeInsets.only(left: ScreenUtil().setWidth(5)),
                alignment: Alignment.centerLeft,
                width: ScreenUtil().setWidth(261),
                height: ScreenUtil().setHeight(40),
                decoration: BoxDecoration(
                    borderRadius:
                        BorderRadius.circular(ScreenUtil().setWidth(4)),
                    border: Border.all(color: Color(0xffDCDFE6))),
                child: MenuWidget(
                  widget.sortItems,
                  widget.selectedSort,
                  call: dialogCall,
                ),
              ),
              Expanded(child: Text("")),
              Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: <Widget>[
                  GestureDetector(
                    child: Container(
                      alignment: Alignment.center,
                      width: ScreenUtil().setWidth(119),
                      height: ScreenUtil().setHeight(40),
                      child: Text(
                        "取消",
                        style: TextStyle(color: Color(0xffF94F68)),
                      ),
                      decoration: redButtonStyleWithBorder,
                    ),
                    onTap: () {
//                        showdialog = false;
//                        setState(() {
//
//                        });
//                        Overlay.of(context).reassemble();
                      NaviUtil.pop(context);
                    },
                  ),
                  GestureDetector(
                    child: Container(
                      alignment: Alignment.center,
                      width: ScreenUtil().setWidth(119),
                      height: ScreenUtil().setHeight(40),
                      child: Text(
                        "确定",
                        style: TextStyle(color: Colors.white),
                      ),
                      decoration: redButtonStyle,
                    ),
                    onTap: () {
                      NaviUtil.pop(context);
                      requetFollow();
                    },
                  )
                ],
              )
            ],
          ),
          decoration: BoxDecoration(
              color: Colors.white, borderRadius: BorderRadius.circular(3)),
        ),
        ScreenUtil().setHeight(215));
  }

  void dialogCall(String groupId) {
    this.selectGroup = groupId;
  }

  _showChangeFollowDialog() {
    DialogUtil.showdialog1(
        this.context,
        Container(
          width: ScreenUtil().setWidth(323),
          height: ScreenUtil().setHeight(362),
          decoration: BoxDecoration(
              color: Colors.white, borderRadius: BorderRadius.circular(4)),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.center,
            children: <Widget>[
              SizedBox(
                height: ScreenUtil().setHeight(24),
              ),
              Text(
                "您是否要更改您的跟随计划",
                style: TextStyle(
                    color: Color(0xff000000),
                    fontSize: ScreenUtil().setSp(16),
                    fontWeight: FontWeight.bold),
              ),
              SizedBox(
                height: ScreenUtil().setHeight(27),
              ),
              Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: <Widget>[
                  CircleAvatar(
                    radius: ScreenUtil().setWidth(21.5),
                    backgroundImage: NetworkImage("${widget.hasfollowUrl}"),
                  ),
                  SizedBox(
                    width: ScreenUtil().setWidth(12),
                  ),
                  Column(
                    children: <Widget>[
                      Text(
                        "${widget.hasfollowName}",
                        style: TextStyle(
                            color: Color(0xff000000),
                            fontSize: ScreenUtil().setSp(18),
                            fontWeight: FontWeight.bold),
                      ),

                    ],
                  )
                ],
              ),
              SizedBox(
                height: ScreenUtil().setHeight(15),
              ),
              Image.asset(
                "assets/assist/change.png",
                width: ScreenUtil().setWidth(44),
                height: ScreenUtil().setHeight(44),
              ),
              SizedBox(
                height: ScreenUtil().setHeight(15),
              ),
              Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: <Widget>[
                  CircleAvatar(
                    radius: ScreenUtil().setWidth(21.5),
                    backgroundImage: widget.official==1?AssetImage("assets/assist/aplus.png"):NetworkImage("${widget.url}"),
                  ),
                  SizedBox(
                    width: ScreenUtil().setWidth(12),
                  ),
                  Column(
                    children: <Widget>[
                      Text(
                        "${widget.name}",
                        style: TextStyle(
                            color: Color(0xff000000),
                            fontSize: ScreenUtil().setSp(18),
                            fontWeight: FontWeight.bold),
                      ),
//                      Container(
//                        alignment: Alignment.center,
//                        width: ScreenUtil().setWidth(56),
//                        height: ScreenUtil().setHeight(14),
//                        decoration: redButtonStyle,
//                        child: Text(
//                          "${widget.desc}",
//                          style: TextStyle(
//                              color: Colors.white,
//                              fontSize: ScreenUtil().setSp(10)),
//                        ),
//                      )
                    ],
                  )
                ],
              ),
              SizedBox(
                height: ScreenUtil().setHeight(29),
              ),
              Row(
                children: <Widget>[
                  SizedBox(
                    width: ScreenUtil().setWidth(33),
                  ),
                  GestureDetector(
                    child: Container(
                      width: ScreenUtil().setWidth(119),
                      height: ScreenUtil().setHeight(40),
                      decoration: redButtonStyleWithBorder,
                      alignment: Alignment.center,
                      child: Text(
                        "取消",
                        style: TextStyle(
                            color: Color(0xffF94F68),
                            fontSize: ScreenUtil().setSp(16)),
                      ),
                    ),
                    onTap: () {
                      NaviUtil.pop(context);
                    },
                  ),
                  SizedBox(
                    width: ScreenUtil().setWidth(15),
                  ),
                  GestureDetector(
                    child: Container(
                      width: ScreenUtil().setWidth(119),
                      height: ScreenUtil().setHeight(40),
                      decoration: redButtonStyle,
                      alignment: Alignment.center,
                      child: Text(
                        "确定",
                        style: TextStyle(
                            color: Colors.white,
                            fontSize: ScreenUtil().setSp(16)),
                      ),
                    ),
                    onTap: () {
                      NaviUtil.pop(context);
                      requetFollow();
                    },
                  ),
                ],
              )
            ],
          ),
        ),
        ScreenUtil().setHeight(362));
  }

  String selectGroup = "";

  void requetPlan() async {
    DialogUtil.showloadingdialog(context);
    try {
      Map<String, dynamic> map = Map<String, dynamic>();

//      map["member_id"] = Constants.memberInfoEntity.data.memberId;
      map["member_id"] = widget.id;

      Map<String, dynamic> map1 = await HttpClient.requestLogin(
          Config.requestApi + 'light/group/getTask',
          data: map);

      SomebodyOfficialPlanEntity loginEntity = new SomebodyOfficialPlanEntity();

      loginEntity = somebodyOfficialPlanEntityFromJson(loginEntity, map1);

      DialogUtil.pop(context);
      if (loginEntity.code == null || loginEntity.code != 200) {
//        Fluttertoast.showToast(msg: "null");
        Toast.show(msg: loginEntity.msg);
         return;
      }
      if (loginEntity.code == 200) {

        taskSend = loginEntity.data.sendTask;

//        NaviUtil.pop(this.context);
         setState(() {

        });
      }
    } catch (e) {
      print(e.toString());
      DialogUtil.pop(context);
    }
  }

  void requetFollow() async {
    DialogUtil.showloadingdialog(context);
    try {
      Map<String, dynamic> map = Map<String, dynamic>();

      map["member_id"] = Constants.memberInfoEntity.data.memberId;
      map["is_official"] = 0;
      map["follow_id"] = widget.id;
      map["group_id"] = Constants.currentGroupId;
      map["is_forever"] = 1;

      Map<String, dynamic> map1 = await HttpClient.requestLogin(
          Config.requestApi + 'light/member/follow',
          data: map);

      CommonResultEntity loginEntity = new CommonResultEntity();

      loginEntity = commonResultEntityFromJson(loginEntity, map1);

      DialogUtil.pop(context);
      if (loginEntity.code == null || loginEntity.code != 200) {
//        Fluttertoast.showToast(msg: "null");
        Toast.show(msg: loginEntity.msg);
         return;
      }
      if (loginEntity.code == 200) {
        Toast.show(msg: "跟随成功");
//        NaviUtil.pop(this.context);
       }
    } catch (e) {
      print(e.toString());
      DialogUtil.pop(context);
    }
  }
}
